#include "stdafx.h"

#include "BankData.h"

#include "AccountServer.h"

namespace PC2
{


AccountServer::AccountServer( ApplicationSettings* apSettings )
	: m_Part(apSettings->m_NodeInfo, apSettings->m_DBPath.c_str(), apSettings->m_RemoteNodeInfo, apSettings->m_Parts)
	, m_Executor(apSettings->m_NodeInfo.m_Name, m_Part.GetDataStore())
{
	printf("AccountServer %s\n", apSettings->m_NodeInfo.m_Name.c_str());
	m_Part.SetExecutor(&m_Executor);
}

void AccountServer::Exec()
{
	int cmd;
	while(true)
	{
		printf("1: dump all record, 2 change record, else start\n");
		scanf("%d", &cmd);
		if(1 == cmd)
		{
			std::vector<BankAccount> lVec;
			m_Executor.DumpAllAcount(&lVec);

			for (auto it = lVec.begin(); it != lVec.end(); it++)
			{
				printf("[name] %s [value] %u\n",it->m_Name, it->m_Value);
			}
		}
		else if(2 == cmd)
		{
			printf("enter [name] [value]:\n");
			char lName[BANK_MAX_LEN];
			uint32_t lValue;
			scanf("%s %u", lName, &lValue);
			m_Executor.SetAccountValueAndStore(lName, lValue);
		}
		else
		{
			m_Part.Run();
		}

	}
}


}